package com.tucanstubs.android_wheresmystuff.activities;
import java.util.Date;

import com.tucanstubs.android_wheresmystuff.R;
import com.tucanstubs.android_wheresmystuff.database.ItemHelper;
import com.tucanstubs.android_wheresmystuff.database.UserHelper;
import com.tucanstubs.android_wheresmystuff.items.*;
import com.tucanstubs.android_wheresmystuff.login.*;

import android.app.Activity;
import android.util.Log;
import android.view.*;
import android.widget.*;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * Activity for creating an item and adding it to the Users item list. Users create an item
 * by filling in the relevant information and clicking the "create" button, to cancel creation
 * the user can click "cancel"
 * @author team 37
 * @version 2.0 4/9/2013
 *
 */
public class AddItemActivity extends Activity{
	private EditText itemName, itemDescription, itemReward;
	private TextView textReward;
	private RadioGroup itemTypeGroup, itemCategory;
	private User user;
	
	/**
	 * Creates the activity. Initializes all GUI Components 
	 * and retrieves information form the bundle.
	 * The activity is passed a bundle with the key Tags.USERNAME mapping to
	 * the user name of the currently logged in user.
	 * 
	 * @param SavedInstanceState not used
	 */
	public void onCreate(Bundle SavedInstanceState){
		super.onCreate(SavedInstanceState);
		setContentView(R.layout.add_item_screen);
		Bundle bundle=getIntent().getExtras();
		user=UserHelper.get(this, bundle.getString(Tags.USERNAME));
		itemName = (EditText) findViewById(R.id.Enter_Item_Name);
		itemDescription = (EditText) findViewById(R.id.Enter_Item_Description);
		itemReward = (EditText) findViewById(R.id.Enter_Item_Reward);
		itemTypeGroup=(RadioGroup) findViewById(R.id.Type_Choices);
		itemCategory=(RadioGroup) findViewById(R.id.Item_Types);
		textReward=(TextView) findViewById(R.id.Item_Reward);
	}
	
	/**
	 * Method that is called when a button is clicked. Either an item is added to the currently
	 * selected user's item list or the activity is cancelled. Either way control is passed back
	 * to HomeScreenActivity
	 * 
	 * @param view The Button which was clicked on
	 */
	public void onClick(View view){
		Item item;
		int selctedCategory=itemCategory.getCheckedRadioButtonId();
		//find out which category is selected
		ItemType itemType;
		switch (selctedCategory){
			case R.id.Heirloom:
				itemType=ItemType.HEIRLOOM;
				break;
			case R.id.KeepSake:
				itemType=ItemType.KEEPSAKE;
				break;
			default:
				itemType=ItemType.MISC;
				break;		
		}
		//set up info for creating an item
		String name=itemName.getText().toString();
		String description=itemDescription.getText().toString();
		double[] location={0,0};
		Date dateCreated=new Date();
		int id=ItemHelper.getNextId(this);
		switch (view.getId()) {
		//if add item was clicked
		case R.id.Create_Item_Button:
			//find out what radio button is selected, and which item type
			//we are creating
			switch(itemTypeGroup.getCheckedRadioButtonId()){
				case(R.id.Lost_Item):
					item=new LostItem(name,description,location,dateCreated,
							Double.parseDouble(itemReward.getText().toString()),itemType,id);
					break;
				case(R.id.Found_Item):
					item=new FoundItem(name, description,location,dateCreated,itemType,id);
					break;
				case(R.id.Donation):
					item=new Donation(name,description,location,dateCreated,id);
					break;
				default: 
					item=new Request(name,description,location,dateCreated,id);
					break;
			}
			Log.i(Tags.ADDITEMACTIVITY, item.toString());
			user.addItem(item);
			UserHelper.add(this, user);
			finish();
		//don't create an item
		case R.id.Cancel_Button:
			finish();
			break;
		}
	}
	
	/**
	 * Method is called when a radio button is clicked. This radio button updates the screen so
	 * only the fields relevant to the selected item type are displayed.
	 * 
	 * @param view The radio button which was clicked
	 */
	public void onRadioButtonClicked(View view) {
	    boolean checked = ((RadioButton) view).isChecked();
	    switch(view.getId()){
	    //find out which button was clicked, and change the screen the user sees
	    //by enabling or disabling the proper fields, and setting their visibility to true or false
	    	case R.id.Lost_Item:
	    		if(checked){
	    			itemReward.setEnabled(true);
	    			itemReward.setVisibility(view.VISIBLE);
	    			itemCategory.setEnabled(true);
	    			itemCategory.setVisibility(view.VISIBLE);
	    			textReward.setVisibility(view.VISIBLE);
	    		}
	    		break;
	    	case R.id.Found_Item:
	    		if(checked){
	    			itemReward.setEnabled(false);
	    			itemReward.setVisibility(view.INVISIBLE);
	    			itemCategory.setEnabled(true);
	    			itemCategory.setVisibility(view.VISIBLE);
	    			textReward.setVisibility(view.INVISIBLE);
	    		}
	    		break;
	    	case R.id.Donation:
	    	case R.id.Request:
	    		if(checked){
	    			itemReward.setEnabled(false);
	    			itemReward.setVisibility(view.INVISIBLE);
	    			itemCategory.setEnabled(false);
	    			itemCategory.setVisibility(view.INVISIBLE);
	    			textReward.setVisibility(view.INVISIBLE);
	    		}
	    		break;
	    }
	}
}
